package
{
	import flash.display.*;
	import flash.events.Event;
	
	public class Mouth extends MovieClip
	{
	
		public var WIDTH:Number, HEIGHT:Number;
		public function get blinking():Boolean{return _blinking;}
		// private objects
		private var unit:Number;
		private var back:Sprite, tounge:Sprite, topteeth:Sprite, bottomteeth:Sprite, lips:Sprite;
		private var _blinking:Boolean = false;
		private var _blinkingProgress:Number = 0;
		private var outlineColor:uint;
		public function blink():void{_blinking = true; _blinkingProgress = 0;}
		
		public function Mouth(WIDTH:Number, HEIGHT:Number, outlineColor:uint)
		{
			unit = ShapeProductionTask.unit;
			this.mouseEnabled = false;
			this.mouseChildren = false;
			this.WIDTH = WIDTH;
			this.HEIGHT = HEIGHT;
			this.outlineColor = outlineColor;
			back = new Sprite();
			tounge = new Sprite();
			topteeth = new Sprite();
			bottomteeth = new Sprite();
			lips = new Sprite();
			
			this.addChild(back);
			back.addChild(tounge);
			back.addChild(topteeth);
			back.addChild(bottomteeth);
			back.addChild(lips);
			
			drawMouthOpen();
			this.addEventListener(Event.ADDED, handleAdded);
		}
		private function handleAdded(event:Event):void
		{
			this.removeEventListener(Event.ADDED, handleAdded);
			this.addEventListener(Event.ENTER_FRAME, handleFrame);
			this.addEventListener(Event.REMOVED, handleRemoved);
		}
		
		private function drawMouthOpen():void
		{
			var backColor:uint = 0x880000;
			back.graphics.lineStyle(unit*HEIGHT/4, outlineColor);
			back.graphics.beginFill(backColor);
			MathUtilities.drawArc(back.graphics,unit*HEIGHT/4, outlineColor, 0, 0, Math.PI/2, -Math.PI/2, WIDTH/2,1);
			back.graphics.lineTo(WIDTH/2, 0);
			back.graphics.endFill();
			
			tounge.graphics.beginFill(0xCC0000);
			tounge.graphics.drawEllipse(-WIDTH*1/3, -HEIGHT/4, WIDTH*2/3, HEIGHT/2);
			tounge.graphics.endFill();
			tounge.y = HEIGHT*2/3;
			
			bottomteeth.graphics.beginFill(0xFFFFFF);
			bottomteeth.graphics.drawRect(-WIDTH*1/3, -HEIGHT/8, WIDTH*2/3, HEIGHT/4);
			bottomteeth.graphics.endFill();
			bottomteeth.y = HEIGHT*4/5;
			
			topteeth.graphics.beginFill(0xFFFFFF);
			topteeth.graphics.drawRect(-WIDTH*1/2, 0, WIDTH, HEIGHT/4);
			topteeth.graphics.endFill();
			topteeth.y = 0;
			
			trace(unit*HEIGHT);
			lips.graphics.lineStyle(unit*HEIGHT/5,outlineColor);
			MathUtilities.drawArc(lips.graphics,unit*HEIGHT/5, outlineColor, 0, 0, Math.PI/2, -Math.PI/2, WIDTH/2,1);
			lips.graphics.lineTo(WIDTH/2,0);			
		}
		
		private function handleFrame (event:Event):void
		{
			
			// _blinking, one second
			if (_blinking)
			{
				_blinkingProgress += .04;
				if (_blinkingProgress < .5)
				{
					this.height -= .08*HEIGHT/2;
				} else if (_blinkingProgress < 1) 
				{	
					this.height += .08*HEIGHT/2;
				} else if (_blinkingProgress >= 1)
				{
					_blinking = false;
				}
			}
		}
		/** remove frame listener */
		private function handleRemoved (event:Event):void
		{
			this.addEventListener(Event.ADDED, handleAdded);
			this.removeEventListener(Event.REMOVED, handleRemoved);
			this.removeEventListener(Event.ENTER_FRAME, handleFrame);
		}
	}
}